package com.cfl.user.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cfl.model.User;
import com.cfl.user.feign.UserFeign;
import com.cfl.user.service.UserService;
import com.cfl.utils.AppResponse;
import com.cfl.utils.Constant;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserFeign userFeign;
	
	public AppResponse login(String name, String password) throws Exception {
		try {
			if(StringUtils.isBlank(name) || StringUtils.isBlank(password))
			{
				// 用户名或密码为空
				return new AppResponse(Constant.code_error, Constant.name_or_password_empty);
			}
			User user = userFeign.findUserByNameAndPassword(name, password);
			if(user == null)
			{
				// 用户名或密码错误
				return new AppResponse(Constant.code_error, Constant.name_or_password_error);
			}
			return new AppResponse(user);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public AppResponse register(String name, String password) throws Exception {
		try {
			if(StringUtils.isBlank(name) || StringUtils.isBlank(password))
			{
				// 用户名或密码为空
				return new AppResponse(Constant.code_error, Constant.name_or_password_empty);
			}
			User user = userFeign.findUserByName(name);
			if(user != null)
			{
				// 用户已存在
				return new AppResponse(Constant.code_error, Constant.user_exist);
			}
			userFeign.addUser(name, password);
			return new AppResponse(userFeign.findUserByName(name));
		} catch (Exception e) {
			throw e;
		}
	}
}
